package com.zjlanyun.erp.interior.production;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.Data;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.rfid.m10.u8.activity.OutPowerActivity;
import com.zjlanyun.erp.utils.Toastor;
import com.zjlanyun.erp.view.BaseRFIDActivity;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;

public class StateView extends BaseRFIDActivity {

    @Bind(R.id.tv_epc) TextView tv_epc;
    @Bind(R.id.tv_FName) TextView tv_FName;
    @Bind(R.id.tv_FQty) TextView tv_FQty;
    @Bind(R.id.tv_FModel) TextView tv_FModel;
    @Bind(R.id.tv_FNumber) TextView tv_FNumber;
    @Bind(R.id.tv_FGongxuName) TextView tv_FGongxuName;
    @Bind(R.id.tv_FOldType) TextView tv_FOldType;
    @Bind(R.id.tv_result) TextView tv_result;
    @Bind(R.id.tv_status) TextView tv_status;
    @Bind(R.id.tv_FBatchNO) TextView tv_FBatchNO;
    @Bind(R.id.tv_FClosed) TextView tv_FClosed;

    private boolean scaning = false;
    private String apiUrl = "";
    private String epc = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.state_view);
        initBaseView();
        initView();
    }

    private void initView() {
        apiUrl = WebApi.production();
        Bundle bundle = getIntent().getExtras();
        title.setText(bundle.getString("title"));
    }

    /**
     * 开始扫描
     */
    public void startScan() {
        tv_status.setText("盘询开始");
        scanNumber = 1;
        scaning = true;
        super.startScan();
    }

    /**
     * 停止扫描
     */
    public void stopScan() {
        tv_status.setText("盘询结束");
        scaning = false;
        super.stopScan();
    }

    public void refreshText() {
        super.refreshText();
        tv_result.setText(String.format("扫描到:%d个", m_curInventoryBuffer.lsTagList.size()));
        if (m_curInventoryBuffer.lsTagList.size() > 0) {
            epc = m_curInventoryBuffer.lsTagList.get(0).getStrEPC().replace(" ", "");
            getProductInfo();
        }
    }

    /**
     * 获取状态信息
     */
    private void getProductInfo() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("epc", epc);
        HttpRequest client = new HttpRequest(mContext, apiUrl, "getProductInfo", JSON.toJSONString(param), true, true);
        client.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                JSONObject data = response.get().optJSONObject("data");
                if (data.optDouble("FCLItemID") == 0 && data.optDouble("FItemID") == 0) {
                    Toastor.getInstance().showShortToast("该卡未绑定任何产品");
                    clean();
                    return;
                }
                tv_epc.setText(data.optString("epc"));
                if (data.optDouble("FItemID") > 0) {
                    tv_FName.setText(data.optString("FName"));
                    tv_FModel.setText(data.optString("FModel"));
                    tv_FNumber.setText(data.optString("FNumber"));
                    tv_FQty.setText(String.valueOf(data.optDouble("FQty")));
                } else {
                    tv_FName.setText(data.optString("FCLName"));
                    tv_FModel.setText(data.optString("FCLModel"));
                    tv_FNumber.setText(data.optString("FCLNumber"));
                    tv_FQty.setText(String.valueOf(data.optDouble("FCLQty")));
                }
                tv_FGongxuName.setText(data.optString("FGongxuName"));
                tv_FOldType.setText(data.optString("FOldType"));
                tv_FBatchNO.setText(data.optString("FBatchNO"));
                tv_FClosed.setText(data.optInt("FClosed") == 0 ? "否":"是");
            }

            @Override
            public void onError(int what, Response<JSONObject> response) {
                clean();
            }
        });
    }

    private void clean() {
        tv_epc.setText("");
        tv_FName.setText("");
        tv_FGongxuName.setText("");
        tv_FModel.setText("");
        tv_FQty.setText("");
        tv_FNumber.setText("");
        tv_FOldType.setText("");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 80 && Build.MODEL.equals(Data.RFID_DEVICE)) {
            if (scaning) {
                stopScan();
            } else {
                startScan();
            }
        } else if ((keyCode == 131 && Build.MODEL.equals(Data.RFID_DEVICE)) || (keyCode == 132 && Build.MODEL.equals(Data.RFID_DEVICE)) || (keyCode == 133 && Build.MODEL.equals(Data.RFID_DEVICE))) {
            Intent intent = new Intent(mContext, OutPowerActivity.class);
            intent.putExtra("from", "RFID" + className);
            startActivity(intent);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
